package com.slz3.business.module.goodsManagement.gooBaseGoodsAttributeValue.steward;

import com.slz3.business.frame.steward.FindJoinSteward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttributeValue.domain.GooBaseGoodsAttributeValue;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttributeValue.domain.GooBaseGoodsAttributeValueCondition;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttributeValue.domain.GooBaseGoodsAttributeValueFactory;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.MultiValueMap;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

public class GooBaseGoodsAttributeValueSpecification {

    public static FindJoin findJoin(MultiValueMap<String, String> params) {
        return new FindJoin(params);
    }

    public static FindJoin findJoin(GooBaseGoodsAttributeValueCondition condition) {
        return new FindJoin(condition);
    }

    private static class FindJoin implements Specification<GooBaseGoodsAttributeValue> {

        private MultiValueMap<String, String> params;

        private GooBaseGoodsAttributeValueCondition condition;

        public FindJoin(MultiValueMap<String, String> params) {
            this.params = params;
            this.condition = GooBaseGoodsAttributeValueFactory.newGooBaseGoodsAttributeValueCondition(params);
        }

        public FindJoin(GooBaseGoodsAttributeValueCondition condition) {
            this.condition = condition;
        }

        @Override
        public Predicate toPredicate(Root<GooBaseGoodsAttributeValue> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

            try {

                final List<Predicate> predicateList = new ArrayList<>();

                if (params != null && params.size() > 0) {
                    predicateList.addAll(FindJoinSteward.appoint(root, cb, GooBaseGoodsAttributeValue.class, params));

                }

                if (VE.isNotEmpty(condition.getCreateTimeOUTSET())) {
                    predicateList.add(cb.greaterThanOrEqualTo(root.get("createTime"), condition.getCreateTimeOUTSET()));
                }
                if (VE.isNotEmpty(condition.getCreateTimeFINISH())) {
                    predicateList.add(cb.lessThanOrEqualTo(root.get("createTime"), condition.getCreateTimeFINISH()));
                }
                if (VE.isNotEmpty(condition.getLastModifyTimeOUTSET())) {
                    predicateList.add(cb.greaterThanOrEqualTo(root.get("lastModifyTime"), condition.getLastModifyTimeOUTSET()));
                }
                if (VE.isNotEmpty(condition.getLastModifyTimeFINISH())) {
                    predicateList.add(cb.lessThanOrEqualTo(root.get("lastModifyTime"), condition.getLastModifyTimeFINISH()));
                }
                if (VE.isNotEmpty(condition.getSerAuditList())) {
                    CriteriaBuilder.In in = cb.in(root.get("serAudit"));
                    condition.getSerAuditList().forEach(item -> in.value(item));
                    predicateList.add(in);
                }

                if (VE.isNotEmpty(condition.getUpperList())) {
                    CriteriaBuilder.In in = cb.in(root.get("upper"));
                    condition.getUpperList().forEach(item -> in.value(item));
                    predicateList.add(in);
                }

                return predicateList.size() > 0 ? cb.and(predicateList.toArray(new Predicate[]{})) : null;

            } catch (Exception e) {

                e.printStackTrace();

                return cb.and(cb.equal(root.get("uuid"), "0"));

            }

        }

    }

}
